import { ref } from "vue";
import { cloneDeep, map } from "lodash";
import {
  basiclineOption,
  buildLineSeriesData,
  buildBarSeriesData,
  handleUnit,
  handleTooltip,
} from "./echartOption";

// 数据处理公共方法
export const handleEchartDataOption = (type, data, unit) => {
  // console.log("data", data);
  // console.log("unit", unit);
  // console.log('type-000---',type);
  // if(type==='line'){
  //   return lineDataHandle(data, unit)
  // }else if(type==='bar'){
  //   return barDataHandle(data, unit)
  // }
  switch (type) {
    case "line":
      return lineDataHandle(data, unit);
    case "bar":
      return barDataHandle(data, unit);
  }
};
const lineDataHandle = (list = [], unit) => {
  const options = cloneDeep(basiclineOption);
  if (list?.length === 0) return;
  options.legend.data = map(list, "name");
  list.forEach((item) => {
    options.xAxis[0].data = map(item?.data, "toi");
    options.series.push(
      buildLineSeriesData(item.name, map(item?.data, "value"))
    );
  });
  // options.legend.show = false;
  options.yAxis[0].axisLabel.formatter = (v) => handleUnit(v, unit);
  options.tooltip.formatter = (data) => handleTooltip(data, unit);
  return options;
};
const barDataHandle = (list = [], unit) => {
  const options = cloneDeep(basiclineOption);
  if (list?.length === 0) return;
  options.legend.data = map(list, "name");
  list.forEach((item) => {
    options.xAxis[0].data = map(item?.data, "toi");
    options.series.push(
      buildBarSeriesData(item.name, map(item?.data, "value"))
    );
  });
  options.xAxis[0].boundaryGap = true;
  options.yAxis[0].axisLabel.formatter = (v) => handleUnit(v, unit);
  options.tooltip.formatter = (data) => handleTooltip(data, unit);
  return options;
};
